<template>
  <div>
    <page-dialog
      :title="dialogInfo.title"
      v-model:visible="shows"
      :width="dialogInfo.width"
      :bt-loading="dialogInfo.btLoading"
      :bt-list="dialogInfo.btList"
    >
      <!-- 条件栏 -->
      <dialog-table
        ref="scanProductTable"
        v-model:data="tableInfo.data"
        :listen-height="false"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :highlight-row="highlightRow"
        :check-box="checkBox"
        :reserve-selection="true"
        :checked-row-list="checkedRowList"
        :checkedList="checkBox ? checkedList : []"
        :pager="false"
        @handleEvent="handleEvent"
      >
      </dialog-table>
    </page-dialog>
  </div>
</template>

<script setup name="selectScanProduct">
import PageDialog from '../PageDialog/index'
import DialogTable from '../DialogTable/index'
const emits = defineEmits()
const { proxy } = getCurrentInstance()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  sacnData: {
    type: Array,
    default: () => {
      return []
    },
  },
  showDialog: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  defaultList: {
    type: Array,
    default: function () {
      return []
    },
  },
  saveEmpty: {
    type: Boolean,
    default: false,
  },
  checkedRowList: {
    type: Array,
    default: function () {
      return []
    },
  },
  checkedList: {
    type: Array,
    default: function () {
      return []
    },
  },
  fieldList: {
    type: Array,
    default: function () {
      return []
    },
  },
})
// 表格相关
const tableInfo = ref({
  data: computed(() => {
    return props.sacnData
  }),
  fieldList: computed(() => {
    let arr = [
      { label: '供应商', value: 'supplierName' },
      {
        label: '单价',
        value: 'price',
        type: 'column',
        formatter: proxy.$fn.TripartiteMethod,
      },
      {
        label: '库存可用数',
        value: 'stockAvailableQty',
        type: 'column',
        formatter: proxy.$fn.qtyFormatter,
      },
      { label: '入库批次号', value: 'storeBatchNumber' },
    ]
    return props.fieldList.length ? props.fieldList : arr
  }),
})
// 弹窗相关
const dialogInfo = ref({
  title: '选择产品',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: !props.saveEmpty,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref('')
const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emits('update:showDialog', newValue) // 更新父组件shows
  },
})
function save() {
  handleClose()
  emits('callback', selectRow.value)
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      if (!proxy.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !selectRow.value
      }
      break
    case 'tableCheck':
      selectRow.value = data
      if (!proxy.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !selectRow.value
      }
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emits('reset')
}
onMounted(() => {
  // hack, 在table创建出来之后再去调用
  proxy.$nextTick(() => {
    if (props.defaultList.length) {
      selectRow.value = props.defaultList
      props.defaultList.forEach((item) => {
        proxy.$refs['scanProductTable'].toggleRowSelection(item)
      })
    }
  })
})
</script>

<style scoped lang="scss"></style>
